package com.itheima.ums.interceptor;

import com.itheima.mall.pojo.Admin;

/**
 * ThreadLocal工具类
 */
public class AdminHolder {

    private static ThreadLocal<Admin> threadLocal = new ThreadLocal<>();

    /**
     * 保存数据到线程
     */
    public static void set(Admin admin) {
        threadLocal.set(admin);
    }

    /**
     * 获取线程中的用户信息
     */
    public static Admin get() {
        return threadLocal.get();
    }


    /**
     * 从当前线程，获取用户对象的id
     */
    public static Long getAdminId() {
        if (threadLocal.get() == null) {
            return null;
        }

        return threadLocal.get().getId();
    }

    /**
     * 从当前线程，获取用户对象用户名
     */
    public static String getAdminName() {
        if (threadLocal.get() == null) {
            return null;
        }

        return threadLocal.get().getUsername();
    }

    /**
     * 移除线程中数据
     */
    public static void remove() {
        threadLocal.remove();
    }



}
